home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 60 / 60.xpi / chrome / webdeveloper.jar / content / webdeveloper / common / validation / accessibility.js next >
Text File  |  2009-06-30  |  13KB  |  355 lines

  1. // Constructs a validate accessibility object
  2. function WebDeveloperValidateAccessibility()
  3. {
  4.     this.file              = null;
  5.     this.formElement       = null;
  6.     this.inputElement      = null;
  7.     this.validationRequest = null;
  8. }
  9.  
  10. // Cleans up
  11. WebDeveloperValidateAccessibility.prototype.cleanUp = function()
  12. {
  13.     this.deleteFile();
  14.  
  15.     // If the validation request is set
  16.     if(this.validationRequest)
  17.     {
  18.         this.validationRequest.abort();
  19.     }
  20. }
  21.  
  22. // Creates a source file
  23. WebDeveloperValidateAccessibility.prototype.createSourceFile = function(uri)
  24. {
  25.     var temporaryDirectory = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("TmpD", Components.interfaces.nsIFile);
  26.  
  27.     // If the temporary directory exists, is a directory and is writable
  28.     if(temporaryDirectory.exists() && temporaryDirectory.isDirectory() && temporaryDirectory.isWritable())
  29.     {
  30.         var fileName   = "";
  31.         var sourceFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  32.  
  33.         // Try to get the host
  34.         try
  35.         {
  36.             fileName = uri.host;
  37.         }
  38.         catch(exception)
  39.         {
  40.             // Do nothing
  41.         }
  42.  
  43.         temporaryDirectory.append("webdeveloper-" + fileName + "-" + new Date().getTime() + ".html");
  44.         sourceFile.initWithPath(temporaryDirectory.path);
  45.  
  46.         return sourceFile;
  47.     }
  48.     else
  49.     {
  50.         webdeveloper_error(document.getElementById("webdeveloper-string-bundle").getFormattedString("webdeveloper_tempDirectoryFailed", [temporaryDirectory.path]));
  51.  
  52.         return null;
  53.     }
  54. }
  55.  
  56. // Deletes the file
  57. WebDeveloperValidateAccessibility.prototype.deleteFile = function()
  58. {
  59.     // If the file is set
  60.     if(this.file)
  61.     {
  62.         // Try to delete the file
  63.         try
  64.         {
  65.             this.file.remove(false);
  66.         }
  67.         catch(exception)
  68.         {
  69.             // Do nothing
  70.         }
  71.  
  72.         this.file = null;
  73.     }
  74. }
  75.  
  76. // Returns the post data
  77. WebDeveloperValidateAccessibility.prototype.getPostData = function()
  78. {
  79.     // Try to get the post data
  80.     try
  81.     {
  82.         var sessionHistory = getWebNavigation().sessionHistory;
  83.         var entry          = sessionHistory.getEntryAtIndex(sessionHistory.index, false).QueryInterface(Components.interfaces.nsISHEntry);
  84.  
  85.         return entry.postData;
  86.     }
  87.     catch(exception)
  88.     {
  89.         return null;
  90.     }
  91. }
  92.  
  93. // Parses the validation results by type
  94. WebDeveloperValidateAccessibility.prototype.parseValidationResultsByType = function(type)
  95. {
  96.     var resultsHTML = this.validationRequest.responseText;
  97.     var count       = resultsHTML.split(type + ".gif").length;
  98.  
  99.     // If the count is greater than 0
  100.     if(count > 0)
  101.     {
  102.         return count - 1;
  103.     }
  104.  
  105.     return 0;
  106. }
  107.  
  108. // Retrieves the HTML
  109. WebDeveloperValidateAccessibility.prototype.retrieveHTML = function()
  110. {
  111.     var converter        = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
  112.     var htmlText         = null;
  113.     var inputStream      = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
  114.     var scriptableStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
  115.  
  116.     converter.charset = webdeveloper_getContentDocument().characterSet;
  117.  
  118.     inputStream.init(this.file, 0x01, 0444, null);
  119.     scriptableStream.init(inputStream);
  120.  
  121.     htmlText = converter.ConvertToUnicode(scriptableStream.read(scriptableStream.available()));
  122.  
  123.     scriptableStream.close();
  124.     inputStream.close();
  125.  
  126.     return htmlText;
  127. }
  128.  
  129. // Saves the HTML
  130. WebDeveloperValidateAccessibility.prototype.saveHTML = function(uri)
  131. {
  132.     var webBrowserPersistInterface = Components.interfaces.nsIWebBrowserPersist;
  133.     var webBrowserPersist          = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(webBrowserPersistInterface);
  134.  
  135.     webBrowserPersist.persistFlags     = webBrowserPersistInterface.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION | webBrowserPersistInterface.PERSIST_FLAGS_FROM_CACHE | webBrowserPersistInterface.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
  136.     webBrowserPersist.progressListener = this;
  137.  
  138.     webBrowserPersist.saveURI(uri, null, uri, this.getPostData(), null, this.file);
  139. }
  140.  
  141. // Submits the background request to validate the accessibility
  142. WebDeveloperValidateAccessibility.prototype.submitBackgroundRequest = function()
  143. {
  144.     var requestBody = "CheckURL=1&URLTest=Your+HTML";
  145.  
  146.     // If the priority 1 preference is set
  147.     if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.wai.priority1", true))
  148.     {
  149.         requestBody += "&p1=1";
  150.     }
  151.  
  152.     // If the priority 2 preference is set
  153.     if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.wai.priority2", true))
  154.     {
  155.         requestBody += "&p2=1";
  156.     }
  157.  
  158.     // If the priority 3 preference is set
  159.     if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.wai.priority3", true))
  160.     {
  161.         requestBody += "&p3=1";
  162.     }
  163.  
  164.     // If the Section 508 preference is set
  165.     if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.section508", true))
  166.     {
  167.         requestBody += "&s508=1";
  168.     }
  169.  
  170.     requestBody                              += "&myHTML=" + encodeURIComponent(this.retrieveHTML());
  171.     this.validationRequest.onreadystatechange = webdeveloper_updatePageAccessibilityValidationDetails;
  172.  
  173.     this.validationRequest.open("post", "http://www.hermish.com/check_this.cfm", true);
  174.  
  175.     // Try to set the request header
  176.     try
  177.     {
  178.         this.validationRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  179.         this.validationRequest.send(requestBody);
  180.     }
  181.     catch(exception)
  182.     {
  183.         // Reset the validation request
  184.         this.validationRequest = new XMLHttpRequest();
  185.     }
  186.  
  187.     this.deleteFile();
  188. }
  189.  
  190. // Submits the form to validate the accessibility
  191. WebDeveloperValidateAccessibility.prototype.submitForm = function()
  192. {
  193.     this.inputElement.value = this.retrieveHTML();
  194.  
  195.     this.deleteFile();
  196.     this.formElement.submit();
  197. }
  198.  
  199. // Validate the accessibility of the given URI in the background
  200. WebDeveloperValidateAccessibility.prototype.validateBackgroundAccessibility = function(uri)
  201. {
  202.     this.file = this.createSourceFile(uri);
  203.  
  204.     // If the validation request is not set
  205.     if(!this.validationRequest)
  206.     {
  207.         this.validationRequest = new XMLHttpRequest();
  208.     }
  209.  
  210.     this.saveHTML(uri);
  211. }
  212.  
  213. // Validate the accessibility of the given URI
  214. WebDeveloperValidateAccessibility.prototype.validateAccessibility = function(uri)
  215. {
  216.     var oldTab            = getBrowser().selectedTab;
  217.     var oldURL            = getBrowser().currentURI.spec;
  218.     var generatedDocument = webdeveloper_generateDocument("");
  219.     var bodyElement       = webdeveloper_getDocumentBodyElement(generatedDocument);
  220.     var imageElement      = generatedDocument.createElement("img");
  221.     var inputElement      = null;
  222.     var pElement          = generatedDocument.createElement("p");
  223.     var stringBundle      = document.getElementById("webdeveloper-string-bundle");
  224.  
  225.     generatedDocument.title = stringBundle.getString("webdeveloper_validateAccessibility");
  226.     this.file               = this.createSourceFile(uri);
  227.     this.formElement        = generatedDocument.createElement("form");
  228.  
  229.     webdeveloper_addGeneratedStyles(generatedDocument);
  230.  
  231.     imageElement.setAttribute("alt", "loading");
  232.     imageElement.setAttribute("src", "chrome://webdeveloper/content/images/content/loading.gif");
  233.     pElement.appendChild(imageElement);
  234.     pElement.appendChild(generatedDocument.createTextNode(stringBundle.getString("webdeveloper_contactingValidator")));
  235.     pElement.setAttribute("class", "loading");
  236.     bodyElement.appendChild(pElement);
  237.  
  238.     this.formElement.setAttribute("action", "http://www.hermish.com/check_this.cfm");
  239.     this.formElement.setAttribute("method", "post");
  240.     this.formElement.setAttribute("style", "display: none");
  241.  
  242.     inputElement = generatedDocument.createElement("input");
  243.  
  244.     inputElement.setAttribute("name", "CheckURL");
  245.     inputElement.setAttribute("type", "hidden");
  246.     inputElement.setAttribute("value", "1");
  247.     this.formElement.appendChild(inputElement);
  248.  
  249.     // If the priority 1 preference is set
  250.     if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.wai.priority1", true))
  251.     {
  252.         inputElement = generatedDocument.createElement("input");
  253.  
  254.         inputElement.setAttribute("name", "p1");
  255.         inputElement.setAttribute("type", "hidden");
  256.         inputElement.setAttribute("value", "1");
  257.         this.formElement.appendChild(inputElement);
  258.     }
  259.  
  260.     // If the priority 2 preference is set
  261.     if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.wai.priority2", true))
  262.     {
  263.         inputElement = generatedDocument.createElement("input");
  264.  
  265.         inputElement.setAttribute("name", "p2");
  266.         inputElement.setAttribute("type", "hidden");
  267.         inputElement.setAttribute("value", "1");
  268.         this.formElement.appendChild(inputElement);
  269.     }
  270.  
  271.     // If the priority 3 preference is set
  272.     if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.wai.priority3", true))
  273.     {
  274.         inputElement = generatedDocument.createElement("input");
  275.  
  276.         inputElement.setAttribute("name", "p3");
  277.         inputElement.setAttribute("type", "hidden");
  278.         inputElement.setAttribute("value", "1");
  279.         this.formElement.appendChild(inputElement);
  280.     }
  281.  
  282.     // If the Section 508 preference is set
  283.     if(webdeveloper_getBooleanPreference("webdeveloper.validate.local.accessibility.section508", true))
  284.     {
  285.         inputElement = generatedDocument.createElement("input");
  286.  
  287.         inputElement.setAttribute("name", "s508");
  288.         inputElement.setAttribute("type", "hidden");
  289.         inputElement.setAttribute("value", "1");
  290.         this.formElement.appendChild(inputElement);
  291.     }
  292.  
  293.     inputElement = generatedDocument.createElement("input");
  294.  
  295.     inputElement.setAttribute("name", "URLTest");
  296.     inputElement.setAttribute("type", "hidden");
  297.     inputElement.setAttribute("value", "Your HTML");
  298.     this.formElement.appendChild(inputElement);
  299.  
  300.     this.inputElement = generatedDocument.createElement("input");
  301.  
  302.     this.inputElement.setAttribute("name", "myHTML");
  303.     this.inputElement.setAttribute("type", "hidden");
  304.     this.formElement.appendChild(this.inputElement);
  305.     bodyElement.appendChild(this.formElement);
  306.  
  307.     // If the open tabs in background preference is set to true
  308.     if(webdeveloper_getBooleanPreference("webdeveloper.open.tabs.background", true))
  309.     {
  310.         getBrowser().selectedTab = oldTab;
  311.     }
  312.  
  313.     this.saveHTML(uri);
  314. }
  315.  
  316. // Called when the progress state changes
  317. WebDeveloperValidateAccessibility.prototype.onStateChange = function(webProgress, request, stateFlags, status)
  318. {
  319.     // If the progress has stopped
  320.     if(stateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
  321.     {
  322.         // If the file is set and exists
  323.         if(this.file && this.file.exists())
  324.         {
  325.             // If the validation request is set
  326.             if(this.validationRequest)
  327.             {
  328.                 this.submitBackgroundRequest();
  329.             }
  330.             else
  331.             {
  332.                 this.submitForm();
  333.             }
  334.         }
  335.     }
  336. }
  337.  
  338. // Indicates the interfaces this object supports
  339. WebDeveloperValidateAccessibility.prototype.QueryInterface = function(id)
  340. {
  341.     // If the query is for a supported interface
  342.     if(id.equals(Components.interfaces.nsISupports) || id.equals(Components.interfaces.nsIWebProgressListener))
  343.     {
  344.         return this;
  345.     }
  346.  
  347.     throw Components.results.NS_NOINTERFACE;
  348. }
  349.  
  350. // Dummy methods requiring implementations
  351. WebDeveloperValidateAccessibility.prototype.onLocationChange = function(webProgress, request, location) {}
  352. WebDeveloperValidateAccessibility.prototype.onProgressChange = function(webProgress, request, currentSelfProgress, maximumSelfProgress, currentTotalProgress, maximumTotalProgress) {}
  353. WebDeveloperValidateAccessibility.prototype.onSecurityChange = function(webProgress, request, state) {}
  354. WebDeveloperValidateAccessibility.prototype.onStatusChange   = function(webProgress, request, status, message) {}
  355.